package smallDemend;
/**
 * 随机验证码
 * 验证码总共四位
 * 每一位都在数字字母中产生
 */

import java.util.Random;
import java.util.Scanner;

public class VerificationCode {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Random r = new Random();
        //创建容器
        char[] chars = new char[62];
        //添加元素
        for (int i = 0; i < chars.length; i++) {
            //将数字放在前10个索引
            if (i < 10) {
                chars[i] = (char) ((int) '0' + i);
                //小写字符放在后面
            } else if (i > 9 && i < (10 + 26)) {
                chars[i] = (char) ((int) 'a' + i - 10);
                //大写字母放在最后
            } else if (i > 35 && i < chars.length) {
                chars[i] = (char) ((int) 'A' + i - 36);
            }
        }
        String str = "";
        while (true) {
            //随机抽取验证字母
            for (int i = 0; i < 4; i++) {
                int index = r.nextInt(chars.length);
                char c = chars[index];
                str += c;
            }
            System.out.println(str);
            System.out.println("请输入验证码：");
            String user = sc.nextLine();
            if (user.equals(str)) {
                System.out.println("验证成功！");
                break;
            } else {
                System.out.println("验证失败！");
            }
        }
    }
}
